<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 先退票 再退款
        // 先支付差价 再退票 再退款
        Schema::create('trip_order_refund', function (Blueprint $table) {
            $table->id('refund_id');
            $table->string('user_id')->comment('用户id');
            $table->string('refund_sn')->comment('退款单号');
            $table->string('out_refund_no')->default('')->comment('外部退款单号，主要是有可能一次退多笔');
            $table->string('origin_order_sn')->default('')->comment('origin_order_sn');
            $table->string('order_sn')->comment('订单单号');
            $table->string('title', 127)->comment('退款标题');
            $table->unsignedDecimal('ticket_price', 10, 2)->default(0)->comment('票价');
            $table->string('need_pay_sn')->default('')->comment('退款金额小于0时，要支付的支付单号');
            $table->unsignedDecimal('refund_amount', 10, 2)->comment('退款金额');
            $table->tinyInteger('refund_state')->default(0)->comment('退款状态 0 受理中 1 退款中 2 已到账- 3 退款失败(重试3次均失败) 4 退票失败');
            $table->tinyInteger('trip_type')->default(0)->comment('出行类型 1 火车 2 飞机 3 酒店... ');
            $table->string('out_refund_id')->default('')->comment('外部退票id');
            $table->string('failed_reason')->default('')->comment('失败原因 ： 退票失败的原因');
            $table->tinyInteger('has_ticket')->comment('是否有票 1 有票 0 无票');
            $table->timestamp('refund_at')->nullable()->comment('退款处理时间');
            $table->timestamp('success_at')->nullable()->comment('退款到账时间');
            $table->string('out_trade_no', 32)->default('')->comment('即支付单号 pay_sn 用于退款时传入');
            $table->unsignedDecimal('pay_amount', 10, 2)->default(0)->comment('支付单支付的总金额 便于退款');
            $table->integer('refund_scope')->default(0)->comment('退款作用域 2 的n次 或 暂未启用');
            $table->tinyInteger('refund_type')->default(0)->comment('退款类型 1用户申请退款 2 无票退款 3 未匹配的坐席 4 改签原票需退款');
            $table->string('sub_order_sn')->default('')->comment('子订单号, 如改签原票需退款,关联改签的订单便于查询以作他用');
            $table->string('refund_desc', 120)->default('')->comment('退款描述 , 如退款需支付 加以描述');
            $table->string('out_order_id', 50)->default('')->comment('服务商关联的退款order id');

            $table->timestamps();
            $table->unique('refund_sn', 'unique_refundsn');
            $table->index(['order_sn', 'user_id'], 'idx_order_user');
            $table->index(['origin_order_sn'], 'idx_origin_order_sn');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('trip_order_refund');
    }
};
